package com.ywu.application.standard.component.service;

import com.ywu.application.standard.menu.data.MenuRepository;
import com.ywu.application.standard.menu.dto.MenuDto;
import com.ywu.application.standard.menu.entity.MenuEntity;
import com.ywu.boot.context.YwuContext;
import com.ywu.boot.context.YwuContextHolder;
import com.ywu.boot.utils.RedisUtils;
import com.ywu.common.constant.CommonConstant;
import com.ywu.common.entity.PageList;
import com.ywu.common.page.PageService;
import com.ywu.common.result.CommonResult;
import com.ywu.application.standard.component.data.ComponentRepository;
import com.ywu.application.standard.component.dto.ComponentDto;
import com.ywu.application.standard.component.entity.ComponentEntity;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * @ClassName ComponentService
 * @Description TODO
 * @Author GroundDemo
 * @Date 2024/3/23 16:55
 * @Version 1.0
 **/
@Service
public class ComponentService {
    @Autowired
    private ComponentRepository componentRepository;

    @Autowired
    private MenuRepository menuRepository;

    private static final String YWU_MENU_REGISTER_QUERY_KEY = "YWU_MENU_REGISTER_QUERY:menu";

    /**
    * @Author GroundDemo
    * @Description 查询组件列表
    * @Date 17:18 2024/3/23
    * @Param [componentDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryComponents(ComponentDto componentDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        componentDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        // 查询数量
        int count = componentRepository.queryPageCount(componentDto);
        // 分页查询
        PageService.execPageStartAndEnd(componentDto.getPageInfo());
        List<ComponentEntity> entityList = componentRepository.queryPageList(componentDto);
        // 构建分页信息
        PageList<ComponentEntity> pageList = new PageList<>();
        pageList.setDataList(entityList);
        pageList.setPageSize(componentDto.getPageInfo().getPageSize());
        pageList.setRecordCount(count);
        pageList.setPageIndex(componentDto.getPageInfo().getPageIndex());
        return CommonResult.success(pageList);
    }

    /**
    * @Author GroundDemo
    * @Description 添加组件
    * @Date 19:43 2024/3/24
    * @Param [componentDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> addComponent(ComponentDto componentDto) {
        ComponentEntity componentEntity = new ComponentEntity();
        BeanUtils.copyProperties(componentDto, componentEntity);
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        componentEntity.createInfo(currentContext.getYwuContextUser().getUserAccount());
        componentEntity.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        componentRepository.createComponent(componentEntity);
        return CommonResult.success(componentEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 修改组件
    * @Date 21:31 2024/3/27
    * @Param [componentDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> updateComponent(ComponentDto componentDto) {
        ComponentEntity componentEntity = new ComponentEntity();
        BeanUtils.copyProperties(componentDto, componentEntity);
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        // 校验当前组件是否存在
        ComponentDto checkDto = new ComponentDto();
        checkDto.setId(componentDto.getId());
        checkDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        ComponentEntity checkResEntity = componentRepository.getComponentEntity(checkDto);
        if (Objects.isNull(checkResEntity) || Objects.isNull(checkResEntity.getId())) {
            return CommonResult.error().setMessage("component not exist").setCode(CommonConstant.ERROR_CODE);
        }
        componentEntity.updateInfo(currentContext.getYwuContextUser().getUserAccount());
        componentRepository.updateComponent(componentEntity);
        // 删除菜单关联的缓存
        MenuDto menuDto = new MenuDto();
        menuDto.setComponentId(componentDto.getId());
        List<MenuEntity> entityList = menuRepository.queryByComponentId(menuDto.getComponentId());
        for (MenuEntity entity : entityList) {
            String routeTo = entity.getRouteTo();
            RedisUtils.deleteHashKey(YWU_MENU_REGISTER_QUERY_KEY, routeTo);
        }
        return CommonResult.success(componentEntity.getId());
    }

    /**
    * @Author GroundDemo
    * @Description 删除组件
    * @Date 21:31 2024/3/27
    * @Param [componentDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> deleteComponent(ComponentDto componentDto) {
        // 校验当前组件是否存在
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        ComponentDto checkDto = new ComponentDto();
        checkDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        checkDto.setId(componentDto.getId());
        ComponentEntity checkResEntity = componentRepository.getComponentEntity(checkDto);
        if (Objects.isNull(checkResEntity) || Objects.isNull(checkResEntity.getId())) {
            return CommonResult.error().setMessage("component not exist").setCode(CommonConstant.ERROR_CODE);
        }
        ComponentEntity componentEntity = new ComponentEntity();
        BeanUtils.copyProperties(componentDto, componentEntity);
        componentRepository.deleteComponent(componentEntity);
        return CommonResult.success(null);
    }

    /**
    * @Author GroundDemo
    * @Description 查询图标组件列表 - 不进行租户隔离
    * @Date 10:43 2024/6/1
    * @Param [componentDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryIconComponents(ComponentDto componentDto) {
        // 查询数量
        int count = componentRepository.queryIconPageCount(componentDto);
        // 分页查询
        PageService.execPageStartAndEnd(componentDto.getPageInfo());
        List<ComponentEntity> entityList = componentRepository.queryIconPageList(componentDto);
        // 构建分页信息
        PageList<ComponentEntity> pageList = new PageList<>();
        pageList.setPageSize(componentDto.getPageInfo().getPageSize());
        pageList.setDataList(entityList);
        pageList.setRecordCount(count);
        pageList.setPageIndex(componentDto.getPageInfo().getPageIndex());
        return CommonResult.success(pageList);
    }

    /**
    * @Author GroundDemo
    * @Description 根据组件ID批量查询组件详情
    * @Date 11:32 2024/8/18
    * @Param [ids]
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.component.standard.entity.ComponentEntity>>
    **/
    public CommonResult<List<ComponentEntity>> batchQueryByIds(List<Integer> ids) {
        List<ComponentEntity> entityList = componentRepository.batchQueryByIds(ids);
        if (CollectionUtils.isEmpty(entityList)) {
            return CommonResult.success(new ArrayList<>());
        }
        return CommonResult.success(entityList);
    }

    /**
    * @Author GroundDemo
    * @Description 查询组件数据，避免全量查询所有数据
    * @Date 14:51 2024/9/26
    * @Param [componentDto]
    * @return com.ywu.common.result.CommonResult<java.lang.Object>
    **/
    public CommonResult<Object> queryComponentsWithNoContent(ComponentDto componentDto) {
        YwuContext currentContext = YwuContextHolder.getCurrentContext();
        componentDto.setTenantId(currentContext.getYwuContextTenant().getTenantId());
        // 查询数量
        int count = componentRepository.queryPageCount(componentDto);
        // 分页查询
        PageService.execPageStartAndEnd(componentDto.getPageInfo());
        List<ComponentEntity> entityList = componentRepository.queryPageListNoContent(componentDto);
        // 构建分页信息
        PageList<ComponentEntity> pageList = new PageList<>();
        pageList.setDataList(entityList);
        pageList.setPageSize(componentDto.getPageInfo().getPageSize());
        pageList.setRecordCount(count);
        pageList.setPageIndex(componentDto.getPageInfo().getPageIndex());
        return CommonResult.success(pageList);
    }

    /**
    * @Author GroundDemo
    * @Description 根据组件ID批量查询组件详情 - 不携带组件数据
    * @Date 11:43 2024/9/27
    * @Param [ids]
    * @return com.ywu.common.result.CommonResult<java.util.List<com.ywu.component.standard.entity.ComponentEntity>>
    **/
    public CommonResult<List<ComponentEntity>> batchQueryByIdsWithNoContent(List<Integer> ids) {
        List<ComponentEntity> entityList = componentRepository.batchQueryByIdsWithNoContent(ids);
        if (CollectionUtils.isEmpty(entityList)) {
            return CommonResult.success(new ArrayList<>());
        }
        return CommonResult.success(entityList);
    }
}
